package com.model.prototype;

import java.util.HashMap;

/**
 * @author: cxt
 * @time: 2022/2/21
 * 形状缓存
 */
public class ShapeCache {

    // 缓存对象，多线程用 ConcurrentHashMap
    private static HashMap<String, Shape> map = new HashMap<>();

    // 根据 id 获取对象
    public static Shape getShape(String id) {
        Shape shape = map.get(id);
        try {
            return (Shape) shape.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 假装这是一个从数据库中查询，结果存到map中方法
    public static void queryData() {
        Square square = new Square("1001");
        map.put(square.getId(), square);

        Rectangle rectangle = new Rectangle("1002");
        map.put(rectangle.getId(), rectangle);

        Circle circle = new Circle("1003");
        map.put(circle.getId(), circle);
    }

}
